package com.example.auth.service;

import com.example.auth.entity.SysUser;
import com.example.auth.mapper.SysUserMapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * custom user details service
 *
 * @author cui haida
 * @version JDK 8
 * 2025/8/4
 */
@Lazy
@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * get user info by username
     *
     * @param username username
     * @return  the user info
     * @throws UsernameNotFoundException if the user is not found, throw this exception
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserMapper.selectByUsername(username);
        if (sysUser == null) {
            throw new UsernameNotFoundException("User not found with username: " + username);
        }
        return sysUser;
    }
}